package gs.wicket.app.base;


import gs.wicket.app.GSApp;
import gs.wicket.app.account.AccountPage;
import gs.wicket.app.index.IndexPage;
import gs.wicket.app.login.LogInPage;
import gs.wicket.app.pages.Kontakt;
import gs.wicket.app.pages.Prodavnice;
import gs.wicket.app.product.Konvertor;
import gs.wicket.app.product.Product;
import gs.wicket.app.product.SearchProduct;
import gs.wicket.app.session.GSSession;
import gs.wicket.app.shop.Nakit;
import gs.wicket.app.shop.Parfemi;
import gs.wicket.app.shop.Pice;
import gs.wicket.app.shop.Satovi;
import gs.wicket.app.shop.ShopingPage;

import java.util.List;

import org.apache.wicket.AttributeModifier;
import org.apache.wicket.Component;
import org.apache.wicket.behavior.AbstractBehavior;
import org.apache.wicket.markup.ComponentTag;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.SubmitLink;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.link.ExternalLink;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.util.value.ValueMap;


public class BasePage extends WebPage {
	
	public BasePage() {

		addLanguageLinks();
		addLogoutLink();

		final TextField searchField = new TextField("q", new Model(""));
		
		final ValueMap searchPars = new ValueMap();
		searchPars.put("q", "Trazi");
				
		Form form = new Form("searchform",new CompoundPropertyModel(searchPars)){
			
			@Override		
			protected void onSubmit() {
				List<Product> productList = GSApp.findProducts(searchField.getModelObjectAsString());
				setResponsePage(new SearchProduct(productList));
				
			}
		};		
		add(form);
		form.add(searchField);	
		
		add(new SubmitLink("searchButton", form) {
			private static final long serialVersionUID = 511246638080207216L;
			{
				add(new AttributeModifier("value", new Model(getLangProperty("base.page.search.text"))));
			}
			@Override
			public void onSubmit() {
				List<Product> productList = GSApp.findProducts(searchField.getModelObjectAsString());
				setResponsePage(new SearchProduct(productList));
				
			}
		});
		
		add(new BookmarkablePageLink("indexPage1", IndexPage.class));

		BookmarkablePageLink homeLink = new BookmarkablePageLink("indexPage2",
				IndexPage.class);
		homeLink.add(new Label("base.page.home",
				getLangProperty("base.page.home")));
		homeLink.add(new AttributeModifier("title", new Model(
				getLangProperty("base.page.home"))));
		add(homeLink);

		BookmarkablePageLink convertorLink = new BookmarkablePageLink(
				"konvertor", Konvertor.class);
		convertorLink.add(new Label("base.page.convertor",
				getLangProperty("base.page.convertor")));
		convertorLink.add(new AttributeModifier("title", new Model(
				getLangProperty("base.page.convertor"))));
		add(convertorLink);

		BookmarkablePageLink parfemiLink = new BookmarkablePageLink("parfemi2",
				Parfemi.class);
		parfemiLink.add(new Label("base.page.povrce",
				getLangProperty("base.page.povrce")));
		parfemiLink.add(new AttributeModifier("title", new Model(
				getLangProperty("base.page.povrce"))));
		add(parfemiLink);

		BookmarkablePageLink picaLink = new BookmarkablePageLink("pica2",
				Pice.class);
		picaLink.add(new Label("base.page.vocep",
				getLangProperty("base.page.vocep")));
		picaLink.add(new AttributeModifier("title", new Model(
				getLangProperty("base.page.vocep"))));
		add(picaLink);

		BookmarkablePageLink nakitLink = new BookmarkablePageLink("nakit2",
				Nakit.class);
		nakitLink.add(new Label("base.page.voce",
				getLangProperty("base.page.voce")));
		nakitLink.add(new AttributeModifier("title", new Model(
				getLangProperty("base.page.voce"))));
		add(nakitLink);

		BookmarkablePageLink storeLink = new BookmarkablePageLink(
				"prodavnice4", Prodavnice.class);
		storeLink.add(new Label("base.page.store",
				getLangProperty("base.page.store")));
		storeLink.add(new AttributeModifier("title", new Model(
				getLangProperty("base.page.store"))));
		add(storeLink);

		BookmarkablePageLink productLink = new BookmarkablePageLink("product1",
				ShopingPage.class);
		productLink.add(new Label("base.page.product",
				getLangProperty("base.page.product")));
		productLink.add(new AttributeModifier("title", new Model(
				getLangProperty("base.page.product"))));
		add(productLink);

		BookmarkablePageLink satoviLink = new BookmarkablePageLink("satovi2",
				Satovi.class);
		satoviLink.add(new Label("base.page.povrcep",
				getLangProperty("base.page.povrcep")));
		satoviLink.add(new AttributeModifier("title", new Model(
				getLangProperty("base.page.povrcep"))));
		add(satoviLink);

		BookmarkablePageLink kontaktLink = new BookmarkablePageLink("kontakt1",
				Kontakt.class);
		kontaktLink.add(new Label("base.page.contact",
				getLangProperty("base.page.contact")));
		kontaktLink.add(new AttributeModifier("title", new Model(
				getLangProperty("base.page.contact"))));
		add(kontaktLink);

		BookmarkablePageLink korisnikLink = new BookmarkablePageLink(
				"account1", AccountPage.class);
		korisnikLink.add(new Label("base.page.user",
				getLangProperty("base.page.user")));
		korisnikLink.add(new AttributeModifier("title", new Model(
				getLangProperty("base.page.user"))));
		add(korisnikLink);

		add(new Label("base.page.weekly.discount",
				getLangProperty("base.page.weekly.discount")));
		add(new Label("base.page.search.banner",
				getLangProperty("base.page.search.banner")));
		add(new Label("base.page.navlakusa",
				getLangProperty("base.page.navlakusa")));
		add(new Label("base.page.partners",
				getLangProperty("base.page.partners")));
		

		BookmarkablePageLink store1Link = new BookmarkablePageLink(
				"prodavnice1", Prodavnice.class);
		store1Link.add(new Label("base.page.store.1",
				getLangProperty("base.page.store.1")));
		store1Link.add(new AttributeModifier("title", new Model(
				getLangProperty("base.page.store.1"))));
		add(store1Link);

		BookmarkablePageLink store3Link = new BookmarkablePageLink(
				"prodavnice3", Prodavnice.class);
		store3Link.add(new Label("base.page.store.3",
				getLangProperty("base.page.store.3")));
		store3Link.add(new AttributeModifier("title", new Model(
				getLangProperty("base.page.store.3"))));
		add(store3Link);

		add(new BookmarkablePageLink("prodavnice2", Prodavnice.class));

		add(new ExternalLink("partneri1", "http://www.jukan.ba"));
		add(new ExternalLink("partneri2", "http://www.agrozemlja.net"));
		add(new ExternalLink("partneri3", "http://zdravlje-prehrana.com"));
		add(new ExternalLink("partneri4", "http://www.zdravlje.hr"));
		add(new ExternalLink("partneri5", "http://www.centar-zdravlja.net"));
		add(new ExternalLink("partneri6", "http://www.zdravakrava.hr/"));
		add(new FeedbackPanel("feedback"));
	}

	private void addMenuItem(WebMarkupContainer menu, final String id, Class page) {
		WebMarkupContainer container = new WebMarkupContainer(id);
		menu.add(container);
		container.add(new AbstractBehavior() {

			private static final long serialVersionUID = 1L;

			@Override
			public void onComponentTag(Component component, ComponentTag tag) {
				WebMarkupContainer wmc = (WebMarkupContainer) component;
				BookmarkablePageLink link = (BookmarkablePageLink) wmc.get(id);
				if (link.linksTo(getPage()))
					tag.put("class", tag.getAttributes().getString("class", "")
							+ " active");
				super.onComponentTag(component, tag);
			}
		});
		container.add(new BookmarkablePageLink(id, page).setAutoEnable(true));
	}

	private void addLanguageLinks() {

		Link langEng = new Link("langEng") {

			private static final long serialVersionUID = 1L;

			@Override
			public void onClick() {
				((GSSession) getSession()).setLanguage("en");
				setResponsePage(IndexPage.class);
			}
		};

		add(langEng);

		Link langBiH = new Link("langBiH") {

			private static final long serialVersionUID = 1L;

			@Override
			public void onClick() {
				((GSSession) getSession()).setLanguage("ba");
				setResponsePage(IndexPage.class);
			}
		};
		add(langBiH);
	}
	private void addLogoutLink() {

		final GSSession session = (GSSession) getSession();
		if(session.getLogovanKorisnik() == null)
		{Link logoutLink = new Link("logoutLink") {

			private static final long serialVersionUID = -4423313003763804375L;

			@Override
			public void onClick() {				
				setResponsePage(LogInPage.class);
			}
		};

		logoutLink.add(new Label("base.page.login.label", "Log in"));

		add(logoutLink);
	}
		else {

			Link logoutLink = new Link("logoutLink") {

				private static final long serialVersionUID = -4423313003763804375L;

				@Override
				public void onClick() {
					getSession().invalidate();
					setResponsePage(IndexPage.class);
				}
			};

			logoutLink.add(new Label("base.page.login.label", "Log out"));

			add(logoutLink);
		}
	}

	public String getLangProperty(String key) {
		return ((GSSession) getSession()).getLangProperty(key);
	}
	
	
}
